package com.museum.demo.Controller;

import com.museum.demo.Entity.MuseumEntity;
import com.museum.demo.Interface.Result;
import com.museum.demo.Service.MuseumService;
import com.museum.demo.Service.UserService;
import io.swagger.annotations.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestMapping(value = "/Museum")
@RestController
public class MuseumController {
    @Autowired
    private MuseumService museumService;

    private static final Logger logger = LoggerFactory.getLogger(MuseumController.class);

    @GetMapping(value = "GetMuseumAll")
    public Result GetMuseumAll(){
        List<MuseumEntity> result = new ArrayList<>();
        result = museumService.GetMuseumList();
        if(result.toString() != "[]"){
            return Result.Success(result);
        }
        return Result.Search_Fail();
    }

    @ApiOperation(value="根据名称查找博物馆，返回查找的博物馆实体")
    @ApiImplicitParams({@ApiImplicitParam(name = "museumName",value = "通过前端的主页搜索框传入的名称")})
    @ApiResponses({@ApiResponse(code = 101,message = "表示查找成功"),
    @ApiResponse(code=306,message = "表示查找失败"),
    @ApiResponse(code=308,message = "表示参数为空，检查用户操作")})
    @RequestMapping(value = "/GetMuseumByName",method = RequestMethod.GET)
    public Result GetMuseumByName(String museumName){
        List<MuseumEntity> result = museumService.SearchMuseum(museumName);

        logger.info("==========================================传入进来了参数！"+museumName+"==========================================");

        if(museumName == null){
            return Result.Param_Null();
        }
        Result respone = Result.Search_Fail();
        if(result != null){
            respone = Result.Success(result);
        }

        return respone;
    }

    @ApiOperation(value = "获取所有博物馆的名称")
    @GetMapping(value = "/GetMuseumNameAll")
    public Result GetMuseumNameAll(){
        List<String> result = new ArrayList<>();
        List<Map<String,String>> respone = new ArrayList<>();
        try {
            result = museumService.GetMuseumNameAll();
            for (int i=0;i<result.size();i++){
                Map<String,String> map = new HashMap<>();
                map.put("value",result.get(i));
                respone.add(map);
            }
            return Result.Success(respone);
        }catch (Exception e){
            e.printStackTrace();
        }
        return Result.Search_Fail();
    }

    @ApiOperation(value = "根据博物馆的属性寻找博物馆列表")
    @GetMapping(value = "/GetMuseumByPosition")
    public Result GetMuseumByProperty(String position){
        Result respone = new Result();
        List<MuseumEntity> result = new ArrayList<>();

        if(position != null) {
            result = museumService.SearchMuseumListByLocal(position);
            logger.info(result.toString());
            if(result.toString() == "[]"){
                return Result.Search_Fail();
            }
            respone = Result.Success(result);
        }else{
            respone = Result.Param_null("根据位置查询博物馆");
        }
        return respone;
    }

    @ApiOperation(value = "获得推荐列表")
    @GetMapping(value = "/GetRecommendList")
    public Result GetRecommendList(){
        List<MuseumEntity> result;
        try{
            result = museumService.GetRecommendList();
            return Result.Success(result);
        }catch (Exception e){
            e.printStackTrace();
        }
        return Result.Search_Fail();
    }

    @ApiOperation(value = "根据区域寻找博物馆名称")
    @PostMapping(value = "/GetMuseumNameByLocal")
    public Result GetMuseumByPosition(@RequestParam String position){
        List<Map<String,String>> result = new ArrayList<>();
        if(position == null){
            return Result.Param_Null();
        }

        try {
            result = museumService.GetMuseumNameByLocal(position);
            return Result.Success(result);
        }catch (Exception e){
            e.printStackTrace();
        }

        return Result.Search_Fail();
    }

    @ApiOperation(value = "获取所有区域的名称")
    @GetMapping(value = "/GetAllPosition")
    public Result GetAllPosition(){
        List<String> result = new ArrayList<>();

        try {
            result = museumService.GetAllPosition();
            return Result.Success(result);
        }catch (Exception e){
            e.printStackTrace();
        }

        return Result.Search_Fail();
    }

    @ApiOperation(value = "用户收藏博物馆")
    @PostMapping(value = "/UserSubscribeMuseum")
    public Result UserSubscribeMuseum(@RequestParam int museumid,@RequestParam int userid){
        int result = museumService.SubscribeMuseum(museumid,userid);
        String respone = "插入了"+result+"行！";
        if(result != 0){
            return Result.Success(respone);
        }

        return Result.Search_Fail();
    }

    @ApiOperation(value = "获取某用户的收藏列表")
    @PostMapping(value = "changeSubscribeStatus")
    public Result changeSubscribeStatus(@RequestParam int museumid,@RequestParam int userid){
        int result = 1;
        result = museumService.ChangeSubscribeStatus(museumid,userid);

        if(result != 0){
            return Result.Success();
        }
        return Result.Search_Fail();
    }
}
